# coding: utf-8

# Copyright (c) 2020-2023 Huawei Technologies Co., Ltd.
# oec-hardware is licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#     http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# Author: @quanren
# Create: 2023-01-10
# Desc: Corerain aiacc test.

import os

class CRAIaccTest():
    def __init__(self, device, logger, command):
        self.device = device
        self.logger = logger
        self.command = command
        self.cr_aiacc_driver_log = os.path.join(self.logger.logdir, 'cr_aiacc_driver.log')
        self.cr_aiacc_DM_log = os.path.join(self.logger.logdir, 'cr_aiacc_DM.log')
        self.cr_aiacc_sample_log = os.path.join(self.logger.logdir, 'cr_aiacc_sample.log')
        self.screen_info_log = os.path.join(self.logger.logdir, 'screen_info.log')

    def get_driver_info(self):
        """
        Get driver info, includings name, version
        """
        self.logger.info("Vendor Info:", terminal_print=True)
        self.command.run_cmd('lspci -vs %s' % self.device.pci)

        self.logger.info("Driver Info:", terminal_print=True)
        driver = self.device.driver
        self.logger.info("Driver Name: %s" % driver)

        driver_version = self.device.get_driver_version()
        if driver_version:
            self.logger.info("Driver Version: %s" % driver_version)
        else:
            self.logger.warning(
                "The driver version information cannot be obtained. Please view it manually.")

    def test_cr_aiacc_sample(self):
        """
        Test screen information for corerain x3a
        Returns:
            bool:
        """
        os.chdir("/opt/cr_aiacc/")
        cmd = self.command.run_cmd(
            "./run > %s " % self.cr_aiacc_sample_log)
        if cmd[2] == 0:
            self.logger.info("Test corerain aiacc screen information succeed.")
        else:
            self.logger.error("Test corerain aiacc screen information failed.")
            return False

        return True
